package com.gh.ok.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.gh.ok.R

class ProgressView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    companion object {
        //分段颜色
        private val SECTION_COLORS = IntArray(3)
    }

    private var mWidth = 0
    private var mHeight = 0
    private var max = 0f
    private var mProgress = 0f
    private lateinit var mPaint: Paint
    private var mSection = 0f
    private var mBackgroundColor = 0

    init {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        val mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressView)
        try {
            mProgress = mTypedArray.getFloat(R.styleable.ProgressView_progress, 0f)
            max = mTypedArray.getFloat(R.styleable.ProgressView_max, 100f)
            mSection = mTypedArray.getFloat(R.styleable.ProgressView_section, 0f)
            mBackgroundColor = mTypedArray.getColor(
                R.styleable.ProgressView_backgroundColor,
                Color.TRANSPARENT
            )
            SECTION_COLORS[0] = mTypedArray.getColor(R.styleable.ProgressView_startColor, Color.RED)
            SECTION_COLORS[1] = mTypedArray.getColor(R.styleable.ProgressView_centerColor, Color.GREEN)
            SECTION_COLORS[2] = mTypedArray.getColor(R.styleable.ProgressView_endColor, Color.BLUE)
        } finally {
            mTypedArray.recycle()
        }
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    }

    /**
     * @param canvas
     */
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint.reset()
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.FILL
        val mCircleRadius = (mHeight / 2).toFloat()
        val mRatio = mProgress / max
        mPaint.color = mBackgroundColor
        val bgRect = RectF(0f, 0f, mWidth.toFloat(), mHeight.toFloat())
        canvas.drawRoundRect(bgRect, mCircleRadius, mCircleRadius, mPaint!!)
        val shader = LinearGradient(
            mWidth * mRatio * mSection, mHeight / 2.0f,
            mWidth * mRatio, mHeight / 2.0f, SECTION_COLORS, null, Shader.TileMode.CLAMP
        )
        mPaint.shader = shader
        val psRect = RectF(0f, 0f, mWidth * mProgress / max, mHeight.toFloat())
        canvas.drawRoundRect(psRect, mCircleRadius, mCircleRadius, mPaint!!)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthSpecMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSpecSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSpecMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSpecSize = MeasureSpec.getSize(heightMeasureSpec)
        mWidth =
            if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
                widthSpecSize
            } else {
                0
            }
        mHeight =
            if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
                dipToPx(15)
            } else {
                heightSpecSize
            }
        setMeasuredDimension(mWidth, mHeight)
    }

    private fun dipToPx(dip: Int): Int {
        val scale = context.resources.displayMetrics.density
        return (dip * scale + 0.5f * if (dip >= 0) 1 else -1).toInt()
    }

    /***
     * 设置当前的进度值
     *
     * @param progress
     */
    var progress: Float
        get() = mProgress
        set(progress) {
            mProgress = if (progress > max) max else progress
            invalidate()
        }


}